//
//  LXStatusView.m
//  微了个小博
//
//  Created by imera on 15/10/12.
//  Copyright (c) 2015年 imera. All rights reserved.
//

#import "LXStatusView.h"
#import "LXRetweetedView.h"
#import "LXUser.h"
#import "LXStatus.h"
#import "LXStatusFrame.h"
#import "UIImageView+WebCache.h"
#import "LXPhotosView.h"

@interface LXStatusView()

/** 用户的头像 */
@property (nonatomic, weak) UIImageView *nameImage;
/** 用户的昵称 */
@property (nonatomic, weak) UILabel *nameLabel;
/** 会员图标 */
@property (nonatomic, weak) UIImageView *vipImage;
/** 微博的时间 */
@property (nonatomic, weak) UILabel *timeLabel;
/** 微博的来源 */
@property (nonatomic, weak) UILabel *sourceLabel;
/** 微博的内容(文字) */
@property (nonatomic, weak) UILabel *contentLabel;
/** 微博的单张配图 */
@property (nonatomic, weak) LXPhotosView *photoImage;
/** 被转发微博的父控件 */
@property (nonatomic, weak) LXRetweetedView *retweetedImage;
@end

@implementation LXStatusView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
        self.image = [UIImage resizeWithName:@"timeline_card_top_background"];
        self.highlightedImage = [UIImage resizeWithName:@"timeline_card_top_background_highlighted"];
        self.userInteractionEnabled = YES;
        
        [self setupSubviews];
    }
    return self;
}

/**
 *  初始化子控件
 */
- (void)setupSubviews
{
    /** 用户的头像 */
    UIImageView *nameImage = [[UIImageView alloc]init];
    self.nameImage = nameImage;
    [self addSubview:nameImage];
    
    /** 用户的昵称 */
    UILabel *nameLabel = [[UILabel alloc]init];
    self.nameLabel = nameLabel;
    [self addSubview:nameLabel];
    
    /** 会员图标 */
    UIImageView *vipImage = [[UIImageView alloc]init];
    self.vipImage = vipImage;
    [self addSubview:vipImage];
    
    /** 微博的时间 */
    UILabel *timeLable = [[UILabel alloc]init];
    self.timeLabel = timeLable;
    [self addSubview:timeLable];
    
    /** 微博的来源 */
    UILabel *sourceLabel = [[UILabel alloc]init];
    self.sourceLabel = sourceLabel;
    [self addSubview:sourceLabel];
    
    /** 微博的内容(文字) */
    UILabel *contentLabel = [[UILabel alloc]init];
    self.contentLabel = contentLabel;
    [self addSubview:contentLabel];
    
    /** 微博的单张配图 */
    LXPhotosView *photoImage = [[LXPhotosView alloc]init];
    self.photoImage = photoImage;
    [self addSubview:photoImage];

    /** 被转发微博的父控件 */
    LXRetweetedView *retweetedView = [[LXRetweetedView alloc]init];
    self.retweetedImage = retweetedView;
    [self addSubview:retweetedView];

}
/**
 *  根据模型设置子控件数据
 */
- (void)setStatusF:(LXStatusFrame *)statusF
{
    _statusF = statusF;
    
    LXStatus *status = statusF.status;
    LXUser *user = status.user;
    
    /** 用户的头像 */
    [self.nameImage sd_setImageWithURL:[NSURL URLWithString:user.profile_image_url] placeholderImage:[UIImage resizeWithName:@"news"]];
    self.nameImage.frame = statusF.profile_image_urlF;
    
    /** 用户的昵称 */
    self.nameLabel.text = user.screen_name;
    self.nameLabel.font = LXStatusNameFont;
    self.nameLabel.frame = statusF.nameF;
    
    /** 会员图标 */
    if (user.mbtype) {
        self.vipImage.hidden = NO;
        self.vipImage.image = [UIImage imageWithName:[NSString stringWithFormat:@"common_icon_membership_level%d", user.mbrank]];
        self.vipImage.frame = statusF.vipViewF;
        
        self.nameLabel.textColor = [UIColor orangeColor];
    } else {
        self.nameLabel.textColor = [UIColor blackColor];
        
        self.vipImage.hidden = YES;
    }
    
    /** 微博的时间 */
    self.timeLabel.text = status.created_at;
    self.timeLabel.font = LXStatusTimeFont;
    self.timeLabel.frame = statusF.created_atF;
    
    /** 微博的来源 */
    self.sourceLabel.text = status.source;
    self.sourceLabel.font = LXStatusTimeFont;
    self.sourceLabel.frame = statusF.sourceF;
    
    /** 微博的内容(文字) */
    self.contentLabel.text = status.text;
    self.contentLabel.font = LXStatusTextFont;
    self.contentLabel.numberOfLines = 0;
    self.contentLabel.frame = statusF.textF;
    
    /** 微博的单张配图 */
    if(status.pic_urls.count){
        self.photoImage.hidden = NO;
        self.photoImage.frame = statusF.pic_urlsF;
        self.photoImage.pic_urls = status.pic_urls;
    }else{
        self.photoImage.hidden = YES;
    }
    
    //设置被转发微博子控件Frame及数据
    self.retweetedImage.frame = statusF.retweetedViewF;
    self.retweetedImage.statusF = statusF;
}

@end
